package Dao.Impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import util.jdbcParameters;
import util.jdbcProperties;

import Dao.BaseDao;
import Dao.Impl.MySqlDaoImpl;

public class MySqlDaoImpl implements BaseDao{
	
	private static MySqlDaoImpl dao = new MySqlDaoImpl();
    public MySqlDaoImpl(){		
	}
	
	public static MySqlDaoImpl getInstance(){
		return dao;
	}
	
	public static Connection getConnection() throws Exception {
		// TODO Auto-generated method stub
		Connection cn = null;		
		jdbcParameters parameters = 
			jdbcProperties.getInstance().getParameters();		
		Class.forName(parameters.getDriver());			
		cn = DriverManager.getConnection(parameters.getUrl(),  
				parameters.getUser(),  parameters.getPassword());		
		return cn;
			
	}


	public boolean execute(String sql) {
		// TODO Auto-generated method stub
		boolean re = false;
		try {
			Connection cn = getConnection();
			PreparedStatement ps = cn.prepareStatement(sql); 
			re = ps.execute(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return re;		
	}

	public ResultSet executeQuery(String sql) {
		// TODO Auto-generated method stub
		ResultSet rs = null;
		try {
			Connection cn = getConnection();
			PreparedStatement ps = cn.prepareStatement(sql);
			rs = ps.executeQuery(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return rs;
	}

}
